package com.lagou.edu.util;

import net.sf.cglib.beans.BeanCopier;
import java.util.List;
import java.util.stream.Collectors;

public class ConvertUtils {
        /**
         * 将原对象的属性值，拷贝到目标对象对应的属性中
         * @param source   原对象
         * @param target   目标对象
         * @param <S>
         * @param <T>
         * @return         属性拷贝完成的目标对象
         */
        public static <S,T> T convert(S source,T target){
            if(source == null || target == null){
                return null;
            }
            BeanCopier copier = BeanCopier.create(source.getClass(),target.getClass(),false);
            T result = target;
            copier.copy(source,result,null);
            return result;
        }

        public static <S,T> T convert(S source, Class<T> target) {
            try {
                return convert(source,target.newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         *拷贝列表
         */
        public static <S,T> List<T> convertList(List<S> source, Class<T> target){
            if(source == null){
                return null;
            }
            return source.stream().map(item -> {
                T result = null;
                try {
                    result = target.newInstance();
                    convert(item,result);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                return result;
            }).collect(Collectors.toList());
        }




    /*private static final Logger LOGGER = LoggerFactory.getLogger(ConvertUtils.class);

    public static <S, T> T convert(S source, Class<T> dest, Function<T, T> function) {
        if (source == null) {
            return null;
        }
        try {
            T result = dest.newInstance();
            final BeanCopier copier = BeanCopier.create(source.getClass(), dest, false);
            copier.copy(source, result, null);
            if (function != null) {
                function.apply(result);
            }
            return result;
        } catch (Exception e) {
            LOGGER.error("convert error", e);
        }
        return null;
    }

    public static <S, T> T convert(S source, Class<T> dest) {
        return convert(source, dest, null);
    }

    public static <S, T> T convert(S source, T dest) {
        if (source == null || dest == null) {
            return null;
        }
        T result = dest;
        final BeanCopier copier = BeanCopier.create(source.getClass(), dest.getClass(), false);
        copier.copy(source, result, null);
        return result;
    }

    public static <S, T> List<T> convertList(List<S> source, Class<T> dest) {
        return convertList(source, dest, null);
    }

    public static <S, T> List<T> convertList(List<S> source, Class<T> dest, ConvertCallback<S, T> callback) {
        if (source == null) {
            return null;
        }
        return source.stream().map(s -> {
            T result = null;
            try {
                result = dest.newInstance();
                convert(s, result);
                if (callback != null) {
                    callback.callback(s, result);
                }
            } catch (InstantiationException | IllegalAccessException e) {
                LOGGER.error("convert error", e);
            }
            return result;
        }).collect(Collectors.toList());
    }

    public interface ConvertCallback<S, D> {
        void callback(S source, D dest);
    }*/
}
